package leetcode;

/**
 * 岛屿的周长
 */
public class cisland {

    public static void main(String[] args) {
        System.out.println(islandPerimeter(new int[][]{{0,1,0,0},{1,1,1,0},{0,1,0,0},{1,1,0,0}}));
    }
    public static int islandPerimeter(int[][] grid) {
        int sum = 0;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                if(grid[i][j] == 1)
                    sum += bfs(i, j, grid);
            }
        }
        return sum;
    }
    public static int bfs(int i, int j ,int[][] grid){
        int s = 0;
        if(i - 1 < 0 || grid[i - 1][j] == 0)
            s++;
        if(j - 1 < 0 || grid[i][j - 1] == 0)
            s++;
        if(i + 1 >= grid.length || grid[i + 1][j] == 0)
            s++;
        if(j + 1 >= grid[0].length || grid[i][j + 1] == 0)
            s++;
        return s;
    }
}
/**
{{0,1,0,0},{1,1,1,0},{0,1,0,0},{1,1,0,0}}
 */
